﻿using ZR.Model;
using ZR.Model.Souvenir;
using ZR.Model.Souvenir.Dto;
using ZR.Service.Souvenir.IService;

namespace ZR.Service.Souvenir
{
    [AppService(ServiceType = typeof(IOrderItemService), ServiceLifetime = LifeTime.Transient)]
    public class OrderItemService : BaseService<OrderItem>, IOrderItemService
    {
        public async Task<int> InsertOrderItemAsync(OrderItem orderItem)
        {
            return await Insertable(orderItem).ExecuteCommandAsync();
        }

        public async Task<int> UpdateOrderItemAsync(OrderItem orderItem)
        {
            return await Updateable(orderItem).ExecuteCommandAsync();
        }

        public async Task<int> DeleteOrderItemAsync(int orderItemId)
        {
            return await Deleteable()
                .Where(it => it.Id == orderItemId)
                .ExecuteCommandAsync();
        }

        public async Task<PagedInfo<OrderItem>> SelectOrderItemWithPageAsync(OrderItemQueryDto orderItemQueryDto)
        {
            RefAsync<int> total = 0;
            var exp = Expressionable.Create<OrderItem>();
            var res = await Queryable()
                .Where(exp.ToExpression())
                .ToPageListAsync(orderItemQueryDto.PageNum, orderItemQueryDto.PageSize, total);
            var page = new PagedInfo<OrderItem>()
            {
                PageIndex = orderItemQueryDto.PageNum,
                PageSize = orderItemQueryDto.PageSize,
                Result = res,
                TotalNum = total
            };
            return page;
        }

        public async Task<OrderItem> SelectOrderItemAsyncById(int orderItemId)
        {
            return await Queryable()
                .Where(it => it.Id == orderItemId)
                .FirstAsync();
        }
    }
}